<template>
  <div>
    <el-dialog
      width="500"
      title="新增员工"
      :visible="isShow"
      @close="closeEvent"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model.trim="form.username" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model.trim="form.mobile" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker v-model="form.timeOfEntry" type="date" />
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select v-model="form.formOfEmployment" placeholder="请输入">
            <el-option
              v-for="item in employees.hireType"
              :key="item.id"
              :value="item.id"
              :label="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model.trim="form.workNumber" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <span v-clickoutside="callbackFn">
            <div ref="treeArea">
              <el-input
                v-model="form.departmentName"
                readonly
                placeholder="请输入"
                @focus="focusEvent"
              />
              <el-tree
                v-if="showTree"
                :data="treeData"
                :props="{ label: 'name' }"
                @node-click="nodeClick"
              />
            </div>
          </span>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker v-model="form.correctionTime" type="date" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="footer">
          <el-button type="primary" @click="submit">确定</el-button>
          <el-button>取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import employees from '@/assets/constant/employees'
import { companyDepartment } from '@/api/department'
import { addSysUser } from '@/api/employees.js'
export default {
  name: 'DialogItem',
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      showTree: false,
      treeData: [],
      employees: employees,
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		姓名
        mobile: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		手机号
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		入职时间
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ], //	number	非必须		聘用形式
        workNumber: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		工号
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ], //	string	非必须		组织名称
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }] //	string	非必须		转正时间
      }
    }
  },
  //   mounted() {
  //     document.addEventListener('click', this.clickEvent)
  //   },
  //   beforeDestroy() {
  //     document.removeEventListener('click', this.clickEvent)
  //   },
  methods: {
    closeEvent() {
      this.$emit('update:isShow', false)
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          // 新增员工
          await addSysUser(this.form)
          this.$message.success('新增员工成功')
          this.closeEvent()
          this.$emit('getData')
          // console.log(result)
        }
      })
    },
    async focusEvent() {
      // 显示el-tree，加载数据
      this.showTree = true
      if (this.treeData.length === 0) {
        const res = await companyDepartment()
        this.treeData = this.changeData(res.data.depts)
        console.log(res)
      }
    },
    // 树形数据转换
    changeData(arr, str = '') {
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 树形组件点击
    nodeClick(data) {
      this.form.departmentName = data.name
      console.log(data)
    },
    // clickEvent(e) {
    //   if (this.isShow&&!this.$refs.treeArea.contains(e.target)) {
    //     this.showTree = false
    //   }
    // }
    // 自定义指令执行
    callbackFn() {
      this.showTree = false
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  text-align: center;
}
</style>
